package org.sith.taskrunner.gwt.client.dataloader;


import org.sith.taskrunner.gwt.client.Configuration;
import org.sith.taskrunner.gwt.client.services.taskrunner.LoadInitDataService;
import org.sith.taskrunner.gwt.client.services.taskrunner.LoadInitDataServiceAsync;
import org.sith.taskrunner.gwt.shared.proxy.TaskToolkitEntityProxy;
import org.sith.taskrunner.gwt.shared.proxy.UserEntityProxy;

import java.util.List;

public class LoadInitDataStateHolder {

    private LoadInitDataServiceAsync rpcService;
    private ExecutionStateWatcher executionStateWatcher;

    public void setExecutionStateWatcher(ExecutionStateWatcher executionStateWatcher) {
        this.executionStateWatcher = executionStateWatcher;
    }

    public enum Step {

        LOAD_CURRENT_USER,
        LOAD_SYSTEM_TOOLKITS,
        LOAD_TOOLKITS,
        END,
    }

    private Step currentStep;

    public LoadInitDataStateHolder() {

    }

    public void startLoad() {
        currentStep = Step.LOAD_CURRENT_USER;
        nextElement();
    }


    public void nextElement() {
        switch (currentStep) {
            case LOAD_CURRENT_USER: {
                LoadDataAsyncCallback<UserEntityProxy> async = new LoadDataAsyncCallback<UserEntityProxy>();
                async.setLoadDataDescription("Load current user");
                async.setResultWatcher(new UserEntityProxyResultWatcher());
                async.setExecutionStateWatcher(executionStateWatcher);

                async.setHasNext(false);
                currentStep = Step.END;
//                currentStep = Step.LOAD_SYSTEM_TOOLKITS;



                rpcService = LoadInitDataService.App.getInstance();
                rpcService.loadCurrentUser(async);

            }
            break;
            case LOAD_SYSTEM_TOOLKITS: {
                LoadDataAsyncCallback<List<TaskToolkitEntityProxy>> async = new LoadDataAsyncCallback<List<TaskToolkitEntityProxy>>();
                async.setLoadDataDescription("Load system toolkist");
                async.setResultWatcher(new SystemToolkitsResultWatcher());
                async.setExecutionStateWatcher(executionStateWatcher);
                async.setHasNext(true);
                rpcService.loadSystemToolkits(async);
                currentStep = Step.LOAD_TOOLKITS;
            }
            break;
            case LOAD_TOOLKITS: {
                LoadDataAsyncCallback<List<TaskToolkitEntityProxy>> async = new LoadDataAsyncCallback<List<TaskToolkitEntityProxy>>();
                async.setLoadDataDescription("Load toolkist");
                async.setResultWatcher(new ToolkitsResultWatcher());
                async.setExecutionStateWatcher(executionStateWatcher);
                rpcService.loadToolkits(Configuration.getCurrentUser(), async);
                currentStep = Step.END;
            }
            break;
            case END: {
            }
            break;
            default: {
                return;
            }

        }


    }


    private static class UserEntityProxyResultWatcher implements ResultWatcher<UserEntityProxy> {

        @Override
        public void dataLoaded(UserEntityProxy data) {
            Configuration.setCurrentUser(data);
        }
    }

    private static class SystemToolkitsResultWatcher implements ResultWatcher<List<TaskToolkitEntityProxy>> {

        @Override
        public void dataLoaded(List<TaskToolkitEntityProxy> data) {
            Configuration.setSystemsToolkit(data);
        }
    }

    public static class ToolkitsResultWatcher implements ResultWatcher<List<TaskToolkitEntityProxy>> {
        @Override
        public void dataLoaded(List<TaskToolkitEntityProxy> data) {
            Configuration.setToolkits(data);
        }
    }
}
